/**
 * @title AttenTeam.java
 * @package com.esun.cheetah.biz.personal.attention
 * @description 
 * @author liugan
 * @date 2012-5-28
 * @copyright 深圳市易讯天空
 */
package com.esun.cheetah.biz.personal.attention;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

import android.os.Handler;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.model.personal.attention.AttenTeamInfoChild;
import com.esun.cheetah.model.personal.attention.AttenTeamParent;
import com.esun.cheetah.network.AbsInferiorityCommunicater;

/**
 * @title AttenTeam
 * @description
 * @author liugan
 */
public class AttenTeam extends AbsInferiorityCommunicater {
	private String lotName;
	private String userName;

	/** 用户关注球对parent list */
	private List<AttenTeamParent> attenTeamInfoPa = new ArrayList<AttenTeamParent>();
	/** 子类list */
	private List<List<AttenTeamInfoChild>> llTeamR = new ArrayList<List<AttenTeamInfoChild>>();

	public AttenTeam(Handler handler, String lotName, String userName) {
		super(handler);
		this.lotName = lotName;
		this.userName = userName;
	}

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> attenTeamMap = new HashMap<String, String>();
		attenTeamMap.put(Key.KEY_ID, BizCode.ATTEN_TEAM);
		attenTeamMap.put(Key.LOT_NAME, lotName);
		attenTeamMap.put(Key.KEY_USERNAME, userName);
		return attenTeamMap;
	}

	public List<AttenTeamParent> getAttenTeamInfoPa() {
		return attenTeamInfoPa;
	}

	public void setAttenTeamInfoPa(List<AttenTeamParent> attenTeamInfoPa) {
		this.attenTeamInfoPa = attenTeamInfoPa;
	}

	public List<List<AttenTeamInfoChild>> getLlTeamR() {
		return llTeamR;
	}

	public void setLlTeamR(List<List<AttenTeamInfoChild>> llTeamR) {
		this.llTeamR = llTeamR;
	}

	@Override
	public void getJsonContent(String serverRes) throws JSONException {
		attenTeamInfoPa.clear();
		llTeamR.clear();
		
		JSONObject object = (JSONObject) new JSONTokener(serverRes).nextValue();
		JSONObject object1 = object.getJSONObject("data");
		if(object1.has("msg")){
			return;
		}
		
		JSONArray jsonArray2 = object1.names();
		String []strDemp=new String[jsonArray2.length()];
		// 将jsonArray 装换为StringAray
		for(int i=0;i<jsonArray2.length();i++){
			String  ch=jsonArray2.getString(i);
			strDemp[i]=ch;
		}
		// 将其他联赛放在末尾
		String top = null;
		for (int i = 0; i < strDemp.length; i++) {
			top= strDemp[i];
			if(top.equals("其他联赛")){
				strDemp[i]=strDemp[strDemp.length-1];
				strDemp[strDemp.length-1]=top;
			}
		}
		
		if(jsonArray2 != null&&strDemp.length>0){
			for (int i = 0; i < strDemp.length; i++) {
				AttenTeamParent atInfoPa = new AttenTeamParent();
				String arrayKey = strDemp[i];
				JSONArray jsonArray3 = object1.getJSONArray(arrayKey);
				atInfoPa.setCount(jsonArray3.length()+"");
				atInfoPa.setLeague(arrayKey);
				ArrayList<AttenTeamInfoChild> attenTeamList = new ArrayList<AttenTeamInfoChild>();
				for (int k = 0; k < jsonArray3.length(); k++) {
					AttenTeamInfoChild atInfo = new AttenTeamInfoChild();
					JSONObject jsonobject = jsonArray3.getJSONObject(k);
					atInfo.setId(jsonobject.getString("id"));
					atInfo.setTeam(jsonobject.getString("team"));
					atInfo.setTeamid(jsonobject.getString("teamid"));
					atInfo.setLogUrl(jsonobject.getString("teamsign"));
					atInfo.setLeague(jsonobject.getString("league"));
					attenTeamList.add(atInfo);
				}
				attenTeamInfoPa.add(atInfoPa);
				llTeamR.add(attenTeamList);
			}
		}	
	}

}
